//
//  HRHearViewController.m
//  Hear
//
//  Created by mac on 2018/4/19.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "HRRecommendViewController.h"
#import "HRRecommendTableViewCell.h"
#import "HRRecommondViewModel.h"

static NSIndexPath *indexNum;


@interface HRRecommendViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    CGFloat cellHeight;
    CGFloat currentIndex;
    
}

@property(nonatomic, strong)HRRecommendTableViewCell *hrrecommendTableViewCell;
@property(nonatomic, strong)UITableView *recommentTableView;
@property(nonatomic, strong)HRRecommondViewModel *hrrecommondVM;
@property(nonatomic, strong)NSMutableArray *contentArr;
@property(nonatomic, strong)AVPlayer *player;

@end

@implementation HRRecommendViewController

-(NSMutableArray *)contentArr
{
    if (!_contentArr) {
        _contentArr = [NSMutableArray arrayWithCapacity:1];
    }
    return _contentArr;
}

-(HRRecommondViewModel *)hrrecommondVM
{
    if (!_hrrecommondVM) {
        _hrrecommondVM = [HRRecommondViewModel new];
    }
    return _hrrecommondVM;
}

-(UITableView *)recommentTableView
{
    if (!_recommentTableView) {
        _recommentTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kFullWidth, kFullHeight) style:UITableViewStylePlain];
        NSLog(@"tableviewHeight = %d",SCREENHEIGHT);
        _recommentTableView.delegate = self;
        _recommentTableView.dataSource = self;
        _recommentTableView.bounces = NO;
        _recommentTableView.pagingEnabled = YES;
        [_recommentTableView reloadData];
        [_recommentTableView registerClass:[HRRecommendTableViewCell class] forCellReuseIdentifier:@"HRRecommendTableViewCell"];
        
    }
    return _recommentTableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavigation];
    cellHeight = SCREENHEIGHT;
//    [self setupView];
//    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:self.recommentTableView];
    
}

/*
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    CGFloat currentPage = scrollView.contentOffset.y/cellHeight;
    DDLog(@"==============scrollViewWillBeginDecelerating滑动了 currentPage = %f",currentPage);
    
    if (currentPage > currentIndex){
        currentIndex++;
        if (currentIndex < 10){

        }
    }else if (currentPage < currentIndex){
        currentIndex--;
        if (currentIndex >= 0){

        }
    }
}

-(void)scrollViewDidEndDecelerating:(UITableView *)tableView{
    int currentPage = tableView.contentOffset.y/cellHeight;
    CGFloat currentYUshu = tableView.contentOffset.y-cellHeight*currentPage;
    DDLog(@"==============scrollViewDidEndDecelerating滑动了 *currentPage = %f",currentPage);
    
    if (currentPage > currentIndex){
        currentIndex++;
        if (currentIndex < 10){
            
        }
    }else if (currentPage < currentIndex){
        currentIndex--;
        if (currentIndex >= 0){
        }
    }
    if (currentIndex >= 0 && currentIndex < 10) {
        DDLog(@"curentIndex = %f",currentIndex);
//            NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:currentIndex inSection:0];
//            [tableView scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
        [tableView scrollToRow:currentIndex inSection:0 atScrollPosition:UITableViewScrollPositionTop animated:YES];

    }
}
*/

#pragma mark  ----- tableview的回调

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SCREENHEIGHT;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    NSArray *numberArr = self.contentArr[1];
//    NSString *numberStr = numberArr[0];
//    NSInteger number = [numberStr integerValue];
    return 4;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    _hrrecommendTableViewCell = [_recommentTableView dequeueReusableCellWithIdentifier:@"HRRecommendTableViewCell"];
    if (!_hrrecommendTableViewCell) {
        _hrrecommendTableViewCell = [_hrrecommendTableViewCell initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HRRecommendTableViewCell"];
    }
//    indexNum = indexPath;
    NSString *pageIndex = [NSString stringWithFormat:@"%d",indexPath.row];
    [self getRecommendDataWithPageIndex:pageIndex];
//    _hrrecommendTableViewCell.model = self.contentArr[0];
    return _hrrecommendTableViewCell;
}

//- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    NSString *pageIndex = [NSString stringWithFormat:@"%d",indexPath.row];
//    [self getRecommendDataWithPageIndex:pageIndex];
//}

#pragma mark ---------- 布置导航栏控件
-(void)setupNavigation
{
    UIButton *titileButton = [UIButton buttonWithType:0];
    titileButton.size = CGSizeMake(100, 30);
    [titileButton setTitle:@"推荐" forState:0];
    [titileButton setTitleColor:[UIColor whiteColor] forState:0];
    titileButton.titleLabel.font = CustomUIFont(17);
    [titileButton addTarget:self action:@selector(titileButtonClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.titleView = titileButton;
    
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    searchBtn.frame = CGRectMake(0, 0, 14, 14);
    [searchBtn setBackgroundImage:[UIImage imageNamed:@"搜索"] forState:UIControlStateNormal];
    [searchBtn addTarget:self action:@selector(search) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *searchBarButton = [[UIBarButtonItem alloc] initWithCustomView:searchBtn];
    
    UIButton *filterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    filterBtn.frame = CGRectMake(0, 0, 14, 14);
    [filterBtn setBackgroundImage:[UIImage imageNamed:@"筛选"] forState:UIControlStateNormal];
    [filterBtn addTarget:self action:@selector(filter) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *filterBarButton = [[UIBarButtonItem alloc] initWithCustomView:filterBtn];
    
    [self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects:searchBarButton,filterBarButton, nil]];
    
    UIButton *createTopicBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [createTopicBtn addTarget:self action:@selector(selfCreataTopic) forControlEvents:UIControlEventTouchUpInside];
    [createTopicBtn setBackgroundImage:[UIImage imageNamed:@"创建话题"] forState:0];
    createTopicBtn.frame = CGRectMake(0, 0, 12, 12);
    UIBarButtonItem *createTopicBarBtn = [[UIBarButtonItem alloc] initWithCustomView:createTopicBtn];
    [self.navigationItem setRightBarButtonItem:createTopicBarBtn];
    
//    [JPUSHService setTags:nil alias:USERID fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias) {
//        DDLog(@"iResCode==%d iAlias====%@",iResCode,iAlias);
//    }];
//播放录音
    _hrrecommendTableViewCell.playRecorderBlock = ^(BOOL selected) {
        NSURL *songUrl = [NSURL URLWithString:_hrrecommendTableViewCell.model.SoundUrl];
        AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:songUrl];
        self.player = [[AVPlayer alloc] initWithPlayerItem:item];
        self.player.volume = 0.8;
        if (selected) {
            [self.player play];
        }
        else
        {
            [self.player pause];
        }
    };
}

#pragma mark ------- 获取推荐页面的数据
-(void)getRecommendDataWithPageIndex:(NSString *)PageIndex
{
    WeakSelf
    HRRecommendRequestModel *recommendRequestModel = [HRRecommendRequestModel new];
    recommendRequestModel.PageSize = 1;
    recommendRequestModel.PageIndex = [PageIndex integerValue];
    recommendRequestModel.CurrentUserLocalId = USERID;
    [self.hrrecommondVM getRecommentContentParam:recommendRequestModel resultArr:^(NSArray *resultArr) {
        NSLog(@" -=-=-=-=-resultArr = %@",resultArr);
        if (weakSelf.contentArr) {
            [weakSelf.contentArr removeAllObjects];
        }
        weakSelf.contentArr = resultArr;
        //        [weakSelf.contentArr addObject:resultArray];
                _hrrecommendTableViewCell.model = weakSelf.contentArr[0];
        //        [weakSelf.recommentTableView reloadData];
        [weakSelf.recommentTableView layoutIfNeeded];
    }];
}

/*
-(void)getRecommendDataWithPageIndex:(NSString *)PageIndex
{
    WeakSelf
    if ([PageIndex isEqualToString:@"2"]) {
        NSLog(@"llllllll");
//        [self.recommentTableView reloadData];
    }
    NSMutableDictionary *getDictionary = [[NSMutableDictionary alloc] initWithCapacity:1];
    [getDictionary setObject:@"1" forKey:@"PageSize"];
    [getDictionary setObject:PageIndex forKey:@"PageIndex"];
    [getDictionary setObject:USERID forKey:@"CurrentUserLocalId"];
    NSString *url = @"SoundInfo/Get_GetSoundHot";
    [self.hrrecommondVM getRecommondDataWithRequstDict:getDictionary andUrl:url andresultArray:^(NSMutableArray *resultArray) {
        NSLog(@"resultArray is %@", resultArray);
        if (weakSelf.contentArr) {
            [weakSelf.contentArr removeAllObjects];
        }
        weakSelf.contentArr = resultArray;
//        [weakSelf.contentArr addObject:resultArray];
//        _hrrecommendTableViewCell.model = weakSelf.contentArr[0];
//        [weakSelf.recommentTableView reloadData];
        [weakSelf.recommentTableView layoutIfNeeded];
    }];
}
*/
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
